package com.dtagri.fjwlw.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.adapter.DeviceControlAdapter;
import com.dtagri.fjwlw.base.BaseFragment;
import com.dtagri.fjwlw.bean.TjAreaMsg;
import com.dtagri.fjwlw.bean.TjNvr;
import com.dtagri.fjwlw.broadcastreceiver.TReceiver;
import com.dtagri.fjwlw.util.HKVideoHelper;

/**
 * 设备控制2
 * 
 * @author wxjn
 * 
 */
public class DeviceControlFragment extends BaseFragment {
	protected ListView listView;

	protected TjAreaMsg tjAreaMsg;
	private ImageView imageView;
	protected DeviceControlAdapter deviceControlAdapter;

	private SurfaceView m_osurfaceView = null;

	HKVideoHelper hKVideoHelper;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View inflate = mInflater
				.inflate(R.layout.layout_list_and_surface, null);
		initView(inflate);
		return inflate;
	}

	@Override
	public void initView(View view) {
		super.initView(view);
		imageView=(ImageView) view.findViewById(R.id.control_img);
		
		tReceiver = new TReceiver(getActivity(), TReceiver.CONTROL_BACK) {

			@Override
			public void onReceive(Context arg0, Intent arg1) {
				hKVideoHelper.stopSinglePlayer();
				hKVideoHelper.Cleanup();
			}
		};

		tjAreaMsg = (TjAreaMsg) getArguments().getSerializable("data");
		listView = (ListView) view.findViewById(R.id.listview);
		deviceControlAdapter = new DeviceControlAdapter(getActivity(), this,
				tjAreaMsg.getSmallAreas());
		listView.setAdapter(deviceControlAdapter);

		m_osurfaceView = (SurfaceView) view.findViewById(R.id.Sur_Player);
		hKVideoHelper = new HKVideoHelper(getActivity(), m_osurfaceView);

		if (tjAreaMsg != null) {
			
//			Integer nvrdataport = Integer.valueOf(tjAreaMsg.getNvrdataport());
//			System.out.println("nvrdataport:"+nvrdataport);
			// 服务器数据不正确
			TjNvr nvr = new TjNvr();
			nvr.setNvrwanip(tjAreaMsg.getNvrwanip());
			nvr.setNvrusername(tjAreaMsg.getNvrusername());
			nvr.setNvruserpwd(tjAreaMsg.getNvrpassword());
			nvr.setAppDataPort(tjAreaMsg.getAppDataPort());
			hKVideoHelper.reLogin(nvr);
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		tReceiver.unregisterReceiver();
	}

	private TReceiver tReceiver;

	public void changeCamera(Integer sschanelnum) {
		m_osurfaceView.setVisibility(View.VISIBLE);
		hKVideoHelper.preview(sschanelnum);
		imageView.setVisibility(View.GONE);
	}

}
